package com.hori3d.rc.bean;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.concurrent.ArrayBlockingQueue;

import com.hori3d.rc.service.PrintTask;

import android.util.Log;

public class Printer {
	/**
	 * 打印机类 
	 * @author liushengxin
	 */

	String cmdSuffix = "\r";//打印模块指令 以\r结尾
   
	//设置 键名
	public static String KEY_PRINTER_IP = "printer_ip";
	public static String KEY_PRINTER_PORT = "printer_port";
	public static String KEY_CAMERA_ID = "camera_id";// 当前进度
	public static String KEY_CAMERA_USERNAME = "camera_username";
	public static String KEY_CAMERA_PASSWORD = "camera_password";
 
	// 命令
	public static String CMD_READ_PRINTER_FIRMWARE = "M899";// 取得固件版本�?
	public static String CMD_POWER_OFF = "M81";// 关机指令
	
	public static String CMD_BEEZER="M300 P";//蜂鸣器+时间毫秒 ：M300 P1000

	public static String CMD_READ_CURRENT_TEMP = "M891";// 当前温度
	public static String CMD_READ_CURRENT_SPEED = "M892";// 当前速度
	public static String CMD_READ_CURRENT_PRINTING_PROCESS = "M893";// 当前进度
	public static String CMD_READ_CURRENT_POSITION = "M894"; // 当前位置

	//移轴指令
	public static String CMD_ALL_AXIS_RESET = "G28"; // 3轴复�?
	public static String CMD_X_AXIS_RESET = "G28 X0"; // X轴复�?
	public static String CMD_Y_AXIS_RESET = "G28 Y0"; // Y轴复�?
	public static String CMD_Z_AXIS_RESET = "G28 Z0"; // Z轴复�?

	public static String CMD_PINTING_PROCESS = "M893"; // 当前打印进度
	public static String CMD_GET_SD_FILES = "M20"; // 当前打印进度
	//调平台
	public static String CMD_ADJUST_PLATFORM_1 = "G28 Z1000\n G28 X300 Y200\n G28 Z0"; //调平台1
	public static String CMD_ADJUST_PLATFORM_2 = "M20"; //调平台2
	public static String CMD_ADJUST_PLATFORM_3 = "M20"; //调平台3
	public static String CMD_ADJUST_PLATFORM_4 = "M20"; //调平台4

	//打印机状态代码
	public static int PRINTER_STATUS_NULL = 0; // 打印机状态未知
	public static int PRINTER_STATUS_HEATING = 3; // 打印机正在加温
	public static int PRINTER_STATUS_PRINTING = 1; // 打印机正在打�?
	public static int PRINTER_STATUS_PAUSING = 2; // 打印机状态暂�?
	public static int PRINTER_STATUS_WAITING = -1; // 打印机状态空�?
 
	
	//速度、温度的增幅
	public static int speed_pace_print = 10; // 打印速度变化�?
	public static int speed_pace_fans = 20; // 风扇速度变化�?
	public static int speed_pace_material = 1; // 出料速度变化�?
	public static int temp_pace = 5; // 温度变化�?

	
	int temp1=-1; 			    // 喷头1温度
	int temp1Target=-1;			// 喷头1目标温度
	int temp2=-1; 			    // 喷头2温度
	int temp2Target=-1;			// 喷头2温度
	int tempBed=-1; 			// 热床温度
	int tempBedTarget=-1;	    // 热床目标温度
 
	
	
	
	int printerStatusCode = PRINTER_STATUS_NULL;   // 打印机状态
	boolean isAutoPowerOff=true;                  //打印完成后自动关机

	public int printingTime = 0;					 //打印已进行时间/分钟
	public	int printingProcess = 0;				 //打印进度 百分�?0~100
	public String printingFile = "";			     //当前正在打印的文件名
    public long heatingfinishedTime;
	String firmwareVersion = "未知";           //打印机固件版本号

	String PRINTER_IP = "192.168.1.142";                   //打印机ip
	int PRINTER_PORT = 8091;					  //打印机端口号
 
    public PrintTask prtTask;

	private int printerStatusCode1=0;
	
	//网络打印
	public String webPrintFilePath="/storage/sdcard0/print/";
	public String webPrintFileRecv="";
	//打印任务相关
 
	/**
	 * 打印过程中的 控制指令（如提高打印速度），不会被立即发送，而是暂存在这个队列中.
	 */
    public  LinkedList cmdQueue=new LinkedList<String>();//
 

    public String getCmdInQueue() {
 	  return (String) cmdQueue.poll();
 		}

    public void putCmdInQueue(String cmd) {
 	  this.cmdQueue.offer(cmd);
 		}
    
    
	int speedPrint, // 打印速度
	    speedFans, // 风扇速度�?
		speedMaterial;// 进料速度
    
    
 
	boolean isConnected;
	public boolean isConnected() {
		return isConnected;
	}

	public void setConnected(boolean isConnected) {
		this.isConnected = isConnected;
	}

	public void loadOption(String ip, String port, String camId, String camUsername, String camPw) {
		setPRINTER_IP(ip);
		setPRINTER_PORT(Integer.parseInt(port));
 
	}

 

	// �? - 查询当前打印进度,返回 ok Print:已打印时间|进度百分�?
	/*
	 * 
	 * M220 S120 --设置打印速度�?120% 0 300 10 刘士营�?2015/10/27 14:38:18
	 * M106 S200 --设置风扇转�?�为200 0 255 20 刘士营�?2015/10/27 14:39:21
	 * M221 S105  --设置材料流量为１０５% 80 120 1
	 * 
	 */
	
	
	public boolean isAutoPowerOff() {
		return isAutoPowerOff;
	}

	public void setAutoPowerOff(boolean isAutoPowerOff) {
		this.isAutoPowerOff = isAutoPowerOff;
	}
	
	
	public String getPRINTER_IP() {
		return PRINTER_IP;
	}

	public void setPRINTER_IP(String pRINTER_IP) {
		PRINTER_IP = pRINTER_IP;
	}

	public int getPRINTER_PORT() {
		return PRINTER_PORT;
	}

	public void setPRINTER_PORT(int pRINTER_PORT) {
		PRINTER_PORT = pRINTER_PORT;
	}

 

 
   //设置打印速度指令
	public static String CMD_SET_SPEED_PRINT(int speed) {
		// if(a>=0&&a<=300)

		int max = 300, min = 0;
		if (speed < min)
			speed = min;
		if (speed > max)
			speed = max;
		return "M220 S" + speed;
	}

	//设置风扇速度指令
	public static String CMD_SET_SPEED_FANS(int speed) {
		int max = 255, min = 0;
		if (speed < min)
			speed = min;
		if (speed > max)
			speed = max;
		return "M106 S" + speed;
	}

	//设置材料速度指令
	public static String CMD_SET_SPEED_MATERIAL(int speed) {
		int max = 120, min = 80;
		if (speed < min)
			speed = min;
		if (speed > max)
			speed = max;
		return "M221 S" + speed;
	}

    //设置喷头温度指令 e_no=0or1 分别代表喷头1 喷头2
	public static String CMD_SET_TEMP_E(int e_no, double temp) {
		if (e_no != 0 && e_no != 1)
			return null;

		int max = 350, min = 0;
		if (temp < min)
			temp = min;
		if (temp > max)
			temp = max;
		return "M104 T" + e_no + " S" + temp;
	}

	//设置热床温度指令
	public static String CMD_SET_TEMP_BED(double temp) {
		int max = 150, min = 0;
		if (temp < min)
			temp = min;
		if (temp > max)
			temp = max;

		return "M140 S" + temp;

	}

	//移轴指令
	public static String CMD_MOVE_AXIS(String direction ,String pace) {
		return "G91\rG1 "+direction+pace+"\rG90\r"; 
	}
	
	
	//喷头出料
	public static String CMD_MOVE_E(String eno, String pace,String acc) {
		return "G91\r"+"T"+eno+"\r G1 E"+pace+acc+"\rG90\r";
	}

 

	public int getPrinterStatusCode() {
		return printerStatusCode1;
	}

	public void setPrinterStatu(int printerStatusCode) {
		this.printerStatusCode1 = printerStatusCode;
	}
 

	public String getPrintingFile() {
		return printingFile;
	}

	public void setPrintingFile(String printingFile) {
		this.printingFile = printingFile;
	}

	public int getPrintingTime() {
		return printingTime;
	}

	public void setPrintingTime(int printingTime) {
		this.printingTime = printingTime;
	}

	
	public int getPrintingProcess() {
		return printingProcess;
	}

	
	
	public void setPrintingProcess(int printingProcess) {
		this.printingProcess = printingProcess;
	}

	/**
	 * 打印进行中�?�暂停都视为 “正在打印�??
	 * @return
	 */
	public boolean isPrinting() {
		return getPrinterStatusCode() == this.PRINTER_STATUS_PRINTING;
	}

	public boolean isPrintingOrPausing() {
		return (getPrinterStatusCode() == this.PRINTER_STATUS_PRINTING)||
				(getPrinterStatusCode() == this.PRINTER_STATUS_PAUSING);
	}



	public int getSpeedPrint() {
		return speedPrint;
	}

	public int getSpeedFans() {
		return speedFans;
	}

	public void setSpeedFans(int speedFans) {
		this.speedFans = speedFans;
	}

	public int getSpeedMaterial() {
		return speedMaterial;
	}

	public void setSpeedMaterial(int speedMaterial) {
		this.speedMaterial = speedMaterial;
	}

	public void setSpeedPrint(int speedPrint) {
		this.speedPrint = speedPrint;
	}

	
	public int getTemp(int eno)
	{
		if(eno==0)
			return getTemp1();
		if(eno==1)
			return getTemp2();
		else return -1;
	}
	
	
	public int getTemp1() {
		return temp1;
	}

	public void setTemp1(int temp1) {
		this.temp1 = temp1;
	}

	public int getTemp1Target() {
		return temp1Target;
	}

	public void setTemp1Target(int temp1Target) {
		this.temp1Target = temp1Target;
	}

	public int getTemp2() {
		return temp2;
	}

	public void setTemp2(int temp2) {
		this.temp2 = temp2;
	}

	public int getTemp2Target() {
		return temp2Target;
	}

	public void setTemp2Target(int temp2Target) {
		this.temp2Target = temp2Target;
	}

	public int getTempBed() {
		return tempBed;
	}

	public void setTempBed(int tempBed) {
		this.tempBed = tempBed;
	}

	public int getTempBedTarget() {
		return tempBedTarget;
	}

	public void setTempBedTarget(int tempBedTarget) {
		this.tempBedTarget = tempBedTarget;
	}

 
	

	public String getFirmwareVersion() {
		return firmwareVersion;
	}

	public void setFirmwareVersion(String firmwareVersion) {
		this.firmwareVersion = firmwareVersion;
	}
	
	
	
	public void startPrinting(String fileName)
	{
		 printingTime = 0;					 //打印已进行时�? 单位为分�?
		 printingProcess = 0;				 //打印进度 百分�?0~100
		 printingFile = fileName;                  //当前正在打印的文件名
		 printerStatusCode=PRINTER_STATUS_PRINTING;                      
	}
	
	public void finishPrinting(String fileName)
	{   
		 if(fileName.contains("toPrint"))
			 ;//改掉文件名字
		   
		   
		 printingTime = 0;					 //打印已进行时�? 单位为分�?
		 printingProcess = 0;				 //打印进度 百分�?0~100
		 printingFile = "";                  //当前正在打印的文件名
		 printerStatusCode=PRINTER_STATUS_WAITING;   
		 
		
	}
	
	
	
	
	


}
